package cool.qingqing.streamapi;

/**
 * @author 高山青
 * @Date 2022/1/3  -   11:19
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * steam常用的方法
 */
public class SteamApiDemo04 {

	public static void main(String[] args) {
		/**
		 * 终结方法
		 */
		//是一样的

		List<Integer> list= Arrays.asList(12,23,42,21,2,52);
		list.stream().forEach(i->{System.out.print(i+"  ");});
		System.out.println();
		list.forEach(i->{System.out.print(i+"  ");});
		System.out.println();

		//count方法获取长度
		long l = list.stream().count();
		System.out.println("集合的长度是"+l);

		/**
		 * 其他方法
		 */
		//filter方法当false的时候则会删除掉集合中的元素
		list.stream().filter(i->i>=20 && i<=30?false:true).forEach(i->System.out.print(i+" "));
		System.out.println();

		//limit限制方法只能截取一定长度
		list.stream().limit(3).forEach(i->System.out.print(i+" "));
		System.out.println();
		//指定最后显示的几个
		list.stream().skip(3).forEach(i->System.out.print(i+" "));
		System.out.println();
		System.out.print("map处理之后的值:");
		//map对每个元素进行加工
		list.stream().map(i->i*i).forEach(i->System.out.print(i+" "));
		System.out.println();
		/**
		 * 添加的是对象类型也可以排序
		 */
		//默认排序
		list.stream().sorted().forEach(i->System.out.print(i+" "));
		System.out.println();
		//逆序排列
		list.stream().sorted((o1,o2)->o2-o1).forEach(i->System.out.print(i+" "));
		System.out.println();

		Student s1=new Student(18,"张三","男");
		Student s2=new Student(21,"李四","男");
		Student s3=new Student(18,"张三","女");

		List<Student> slist=new ArrayList<>();
		slist.add(s1);slist.add(s2);slist.add(s3);
		slist.stream().distinct().forEach(i->System.out.print(i+" "));





	}
}
class Student{
	private int age;
	private String name;
	private String sex;

	public Student() {
	}

	public Student(int age, String name, String sex) {
		this.age = age;
		this.name = name;
		this.sex = sex;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Student student = (Student) o;
		return age == student.age && name.equals(student.name);
	}

	@Override
	public int hashCode() {
		return Objects.hash(age, name);
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Student{" +
				"age=" + age +
				", name='" + name + '\'' +
				", sex='" + sex + '\'' +
				'}';
	}
}
